//
//  WNBonusVC.swift
//  WalkNovel
//
//  Created by Kevin on 2023/10/15.
//

import UIKit

class WNBonusVC: WNBaseVC {
    
    lazy var bonusTabView: WNTableView = {
        let tab = WNTableView()
        tab.register(BonusCell.self, forCellReuseIdentifier: BonusCell.identifier)
        tab.backgroundColor = .init(named: "white_F7F7F7")
        tab.delegate = self
        tab.dataSource = self
        tab.rowHeight = 78
        tab.ly_emptyView = WNEmptyView.wn_loadStyle(descStr: .common, imageStr: .common, tapBlock: { [weak self] in
            tab.mj_header?.beginRefreshing()
        })
        tab.isHidden = true
        return tab
    }()
    
    var bonusArr = [OrderDetailModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewConfig() {
        view.addSubview(bonusTabView)
        bonusTabView.snp.makeConstraints { make in
            make.top.equalTo(NaviTotalHeight)
            make.left.bottom.right.equalTo(0)
        }
    }
    
    override func dataConfig() {
        pageName = "獎金歷史"
        navView.titleName = "獎金歷史"
        listSettingRefresh()
        bonusRequest()
    }
}

extension WNBonusVC {
    
    func listSettingRefresh() {
        bonusTabView.mj_header = wn_refreshHeader(completion: { [weak self] in
            self?.page = 1
            self?.hasPage = false
            self?.bonusRequest()
        })
        
        bonusTabView.mj_footer = wn_refreshFooter(completion: { [weak self] in
            if self?.page == 1 && (self?.bonusArr.count ?? 0) == 0 {
                self?.bonusTabView.mj_footer?.endRefreshing()
                return
            }
            if self?.hasPage == true {
                self?.page += 1
                self?.bonusRequest()
            }else {
                self?.bonusTabView.mj_footer?.endRefreshingWithNoMoreData()
            }
        })
    }
}

extension WNBonusVC: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        bonusArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: BonusCell.identifier, for: indexPath) as! BonusCell
        cell.model = bonusArr[indexPath.item]
        cell.lineView.isHidden = indexPath.item == (bonusArr.count - 1)
        return cell
    }
}

extension WNBonusVC {
    
    func bonusRequest() {
        provider_setting.rx.request(.bonusList(page))
            .asObservable().mapModel(BaseModel<[OrderDetailModel]>.self)
            .subscribe(onNext: { [weak self] (model) in
                self?.bonusTabView.isHidden = false
                self?.bonusTabView.ly_emptyView.imageStr = EmptyImage.common.rawValue
                self?.bonusTabView.ly_emptyView.detailStr = EmptyDesc.common.rawValue
                self?.bonusTabView.ly_emptyView.btnTitleStr = ""
                guard model.code == 0 else {
                    if (self?.isFirst ?? true) {
                        self?.bonusTabView.ly_emptyView.imageStr = EmptyImage.serverBad.rawValue
                        self?.bonusTabView.ly_emptyView.detailStr = EmptyDesc.serverBad.rawValue
                        self?.bonusTabView.ly_emptyView.btnTitleStr = "點擊刷新"
                    }else {
                        self?.wn_toast(model.msg ?? "")
                    }
                    self?.bonusTabView.mj_header?.endRefreshing()
                    self?.bonusTabView.mj_footer?.endRefreshing()
                    self?.isFirst = false
                    return
                }
                if self?.page == 1 {
                    self?.bonusArr.removeAll()
                    self?.bonusTabView.mj_footer?.resetNoMoreData()
                }
                if let arr = model.data {
                    self?.hasPage = arr.count > 9
                    self?.bonusArr += arr
                }else {
                    self?.bonusTabView.mj_footer?.endRefreshingWithNoMoreData()
                }
                self?.bonusTabView.reloadData()
                self?.bonusTabView.mj_header?.endRefreshing()
                self?.bonusTabView.mj_footer?.endRefreshing()
                self?.isFirst = false
            }, onError: { [weak self] (error) in
                self?.wn_toast("檢查網路連線")
                self?.bonusTabView.ly_emptyView.imageStr = EmptyImage.netWork.rawValue
                self?.bonusTabView.ly_emptyView.detailStr = EmptyDesc.netWork.rawValue
                self?.bonusTabView.ly_emptyView.btnTitleStr = "點擊刷新"
                self?.bonusTabView.mj_header?.endRefreshing()
                self?.bonusTabView.mj_footer?.endRefreshing()
                self?.bonusTabView.isHidden = false
            }).disposed(by: rx.disposeBag)
    }
}
